Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.jena;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
19: import cz.cvut.kbss.ontodriver.model.Assertion;
20: import cz.cvut.kbss.ontodriver.model.Axiom;
21: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
22: import cz.cvut.kbss.ontodriver.model.Value;
23: import cz.cvut.kbss.ontodriver.util.Vocabulary;
24: import org.apache.jena.rdf.model.*;
25:
26: import java.net.URI;
27: import java.util.*;
28:
29: class ExplicitAxiomLoader extends AbstractAxiomLoader {
30:
31: private static final Assertion UNSPECIFIED_ASSERTION = Assertion.createUnspecifiedPropertyAssertion(false);
32:
33: private final StorageConnector connector;
34:
35: private Map<String, Assertion> assertedProperties;
36: private Assertion unspecifiedProperty;
37:
38: ExplicitAxiomLoader(StorageConnector connector) {
39: this.connector = connector;
40: }
41:
42: @Override
43: boolean contains(Resource subject, Property property, RDFNode object, URI context) {
44: return connector.contains(subject, property, object, context != null ? context.toString() : null);
45: }
46:
47: @Override
48: Collection<Axiom<?>> find(AxiomDescriptor descriptor, Map<String, Assertion> assertions) {
49: this.assertedProperties = assertions;
50: this.unspecifiedProperty = resolveUnspecifiedProperty();
51: final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString());
52: final Collection<Statement> statements = findStatements(subject, null, descriptor.getSubjectContext());
53: final List<Axiom<?>> result = transformStatementsToAxioms(descriptor, statements);
54: result.addAll(loadAxiomsForPropertiesInContext(descriptor, subject));
55: return result;
56: }
57:
58: private Assertion resolveUnspecifiedProperty() {
59: final Optional<Assertion> unspecified =
60: assertedProperties.values().stream().filter(a -> a.equals(UNSPECIFIED_ASSERTION)).findAny();
61: return unspecified.orElse(null);
62: }
63:
64: @Override
65: Collection<Statement> findStatements(Resource subject, Property property, URI context) {
66: return connector.find(subject, property, null, context != null ? context.toString() : null);
67: }
68:
69: private List<Axiom<?>> transformStatementsToAxioms(AxiomDescriptor descriptor, Collection<Statement> statements) {
70: final List<Axiom<?>> axioms = new ArrayList<>(statements.size());
71: for (Statement statement : statements) {
72: final Property property = statement.getPredicate();
73: if (shouldSkipProperty(property, descriptor)) {
74: continue;
75: }
76: final Assertion a =
77: assertedProperties.containsKey(property.getURI()) ? assertedProperties.get(property.getURI()) :
78: createAssertionForStatement(statement);
79: final Optional<Value<?>> value = resolveValue(a, statement.getObject());
80: value.ifPresent(v -> axioms.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
81: }
82: return axioms;
83: }
84:
85: private boolean shouldSkipProperty(Property property, AxiomDescriptor descriptor) {
86: final String propertyUri = property.getURI();
87: // If the property is not mapped and either there is not the unspecified property or the property is rdf:type,
88: // which is handled by Types
89: if (!assertedProperties.containsKey(propertyUri) && (unspecifiedProperty == null || propertyUri
90: .equals(Vocabulary.RDF_TYPE))) {
91: return true;
92: }
93: final Assertion a = assertedProperties.getOrDefault(propertyUri, unspecifiedProperty);
94: return !assertionContextSameAsSubject(descriptor.getSubjectContext(), descriptor.getAssertionContext(a));
95: }
96:
97: private List<Axiom<?>> loadAxiomsForPropertiesInContext(AxiomDescriptor descriptor, Resource subject) {
98: final List<Axiom<?>> axioms = new ArrayList<>();
99: for (Assertion a : assertedProperties.values()) {
100: final URI assertionCtx = descriptor.getAssertionContext(a);
101: if (assertionContextSameAsSubject(descriptor.getSubjectContext(), assertionCtx)) {
102: continue;
103: }
104: final Property property = ResourceFactory.createProperty(a.getIdentifier().toString());
105: final Collection<Statement> statements = findStatements(subject, property, assertionCtx);
106: statements.forEach(statement -> {
107: final Optional<Value<?>> value = resolveValue(a, statement.getObject());
108: value.ifPresent(v -> axioms.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
109: });
110: }
111: if (unspecifiedProperty != null && !assertionContextSameAsSubject(descriptor.getSubjectContext(),
112: descriptor.getAssertionContext(unspecifiedProperty))) {
113: final Collection<Statement> statements =
114: findStatements(subject, null, descriptor.getAssertionContext(unspecifiedProperty));
115: for (Statement s : statements) {
116: final Assertion a = createAssertionForStatement(s);
117: final Optional<Value<?>> value = resolveValue(a, s.getObject());
118: value.ifPresent(v -> axioms.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
119: }
120: }
121: return axioms;
122: }
123:
124: private static boolean assertionContextSameAsSubject(URI subjectCtx, URI assertionCtx) {
125: return assertionCtx == null && subjectCtx == null || (subjectCtx != null && subjectCtx.equals(assertionCtx));
126: }
127: }